package main

//下载地址：https://www.mongodb.com/try/download/community

//打开客户端:mongo.exe

//创建数据库：use go_db;

//创建集合：db.createCollection("student");
//查看集合：db.student.find();
//删除集合数据：db.student.remove({})

//下载驱动： go get go.mongodb.org/mongo-driver/mongo

import (
	"context"
	"fmt"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"log"
)

var client *mongo.Client

func initDB() {
	//设置客户端
	co := options.Client().ApplyURI("mongodb://127.0.0.1:27017")
	//连接
	var err error //error 默认为nil
	c, err := mongo.Connect(context.TODO(), co)
	if err != nil {
		log.Fatal(err)
	}
	//检查连接
	err = c.Ping(context.TODO(), nil)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("Connected to MongoDB!")
	client = c
}
func main() {
	initDB()
}
